<template>
  <el-dialog :title="title" :visible.sync="visible" :width="width" :close-on-click-modal='false' class="bonus">
    <el-row>
      <el-form  :model="formData" ref="formRef" :label-width="labelWidth"  :rules="rules" v-loading="formLoading" :close-on-click-modal="false">
        <el-form-item label="组名称" prop="groupName">
          <el-input v-model="formData.groupName" autocomplete="off" placeholder="请输入组名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="组系数" prop="groupRatio">
          <el-input v-model="formData.groupRatio" autocomplete="off" placeholder="请输入组系数" clearable></el-input>
        </el-form-item>
      </el-form>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary"  @click="handleSubmit">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addGroupObj, putGroupObj} from '@/api/bonus/calculateorgconfig' ;
export default {
  name: "orgForm",
  components:{
  },
  watch: {

  },
  data(){
    return{
      type:'add',
      formLoading:false,
      title:'新增',
      width:'30%',
      visible: false,
      labelWidth:'125px',
      formData: {},
      rules: {
        groupName: [{ required: true, message: '请输入组名称', trigger: 'blur' },],
        groupRatio: [{ required: true, message: '请输入组系数', trigger: 'blur' },],
      },
    }
  },
  methods:{
    init(type,parentData){
      this.type = type
      if (type == 'edit'){
        this.title = '编辑'
        this.formData = parentData ;
      }else if(type == 'add'){
        this.title = '新增'
        this.formData = {} ;
        this.formData.groupRatio = '1'
      }
      this.visible = true ;
    },
    handleSubmit(){
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.formLoading = true ;
          if (this.type == 'add'){
            addGroupObj(this.formData).then(res=>{
              this.formLoading = false ;
              this.handleCancel(true) ;
            }).catch(()=>{
              this.formLoading = false ;
            })
          }else{
            putGroupObj(this.formData).then(res=>{
              this.formLoading = false ;
              this.handleCancel(true) ;
            }).catch(()=>{
              this.formLoading = false ;
            })
          }

        } else {
          return false;
        }
      });
    },
    handleCancel(refresh){
      this.visible = false ;
      this.formData = {} ;
      if (refresh){
        this.$emit('refresh')
      }
      this.$refs.formRef.resetFields() ;
    },
    nodeClick(item){
      this.formData.parentDeptName = item.name
    },
    onChange(){

    }
  }
}
</script>

<style scoped>

</style>
